﻿using System;
using System.Collections;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Xml;

using Aulac.Global.Enums; using Aulac.Global.Data; using Aulac.Global.Common; using Aulac.Global.Xml;
using Aulac.Rsc.Ems; using Aulac.Rsc.Ems.Tags;
using Aulac.Srv.Db.Provider; using Aulac.Srv.Db.Connect;
using Aulac.Srv.Db.Data.Catalogs;

namespace Aulac.Srv.Db.Data.Ems.Systems
{
	public class UserGroupInfo: DbDataCatalogs
	{
        public UserGroupInfo(IDbAulacConnect p_Db) :
            base(p_Db, TableNames.Sys_UserGrp, 
            tagSystem.Users.UserGrpID, 
            new string[]{tagSystem.Users.UserName}
            )
        {
            mListFieds = new System.Collections.Generic.List<string>(
                 new string[] { tagSystem.Users.UserName, tagSystem.Users.UserPassword, tagSystem.Users.UserGrpID, tagSystem.Users.DateRegister, tagSystem.Users.Note });
        }

        protected override void OnGetListItem(ref DataTable pDataReturn, XmlDataDocument xmlParams)
        {
            sqlQuery = string.Format("SELECT UserGrpID, Note, IsSystem FROM {0} GROUP BY UserGrpID, Note, IsSystem", TableNames.Sys_UserGrps);
            DbDataProvider.ExecuteReaderDataToTable(mDbCmd, sqlQuery, ref pDataReturn);
        }

        #region UserGroup
        
        private bool IsGrpAdmin(string UserGrpID)
        {
            sqlQuery = "SELECT Top 1 * FROM Sys_UserGrp WHERE UserGrpID LIKE N'" + UserGrpID + "' AND IsSystem LIKE 'true'";
            mDbCmd.CommandText = sqlQuery;
            bool isAdmin = false;
            DbDataReader = mDbCmd.ExecuteReader();
            if (DbDataReader.Read())
                isAdmin = true;
            DbDataReader.Close();
            return isAdmin;

        }
        
        public XmlDataDocument GetUserGrp(XmlDataDocument xmlInDoc)
        {
            string sUserGrpID = XmlProcess.GetValueParam(xmlInDoc, tagSystem.Users.UserGrpID);

            if (!Aulac.Global.Common.ValueHelper.isEmpty(sUserGrpID) && !IsGrpAdmin(sUserGrpID))
                
                sqlQuery = "SELECT s.UserGrpID, s.IsAccRight, s.IsAdd, s.IsEdit, s.IsDelete, " +
"s.IsPrint, s.IsShow, s.ModuleID, s.IsSystem, v.ModuleName " +
"FROM  Sys_modules v INNER JOIN Sys_UserGrp s " +
"ON s.ModuleID = v.ModuleID WHERE UserGrpID LIKE N'" + sUserGrpID + "' " +
"GROUP BY s.UserGrpID, s.IsAccRight, s.IsAdd, s.IsEdit, " +
"s.IsDelete, s.IsPrint, s.IsShow, s.ModuleID, s.IsSystem, v.ModuleName " +
"UNION ALL " +
"SELECT '' AS UserGrpID,  'False' as  IsAccRight ,  'False' as  IsAdd, '0' as IsEdit, " +
"'0' as IsDelete, 'false' As IsPrint, '0' as IsShow, s.ModuleID, 'False' as IsSystem, " +
"s.ModuleName FROM Sys_modules s " +
"WHERE s.ModuleID NOT IN (SELECT  s.ModuleID FROM  Sys_modules v INNER JOIN Sys_UserGrp s " +
"ON s.ModuleID = v.ModuleID WHERE UserGrpID LIKE N'" + sUserGrpID + "') AND IsActive = 1";
            else if (IsGrpAdmin(sUserGrpID))
                sqlQuery = "SELECT  ModuleID, ModuleName, Location, 'true' AS IsSystem, 'true' AS IsAccRight, 'true' AS IsAdd, 1 AS IsEdit, 1 AS IsDelete, 1 AS IsShow, 'true' AS IsPrint " +
                    "FROM	Sys_Modules WHERE IsActive = 1";
            else
                sqlQuery = "SELECT  ModuleID, ModuleName, Location, 'false' AS IsSystem, 'false' AS IsAccRight, 'false' AS IsAdd, 0 AS IsEdit, 0 AS IsDelete, 0 AS IsShow, 'false' AS IsPrint " +
                    "FROM Sys_Modules WHERE IsActive = 1";

            DbDataReader = DbDataProvider.ExecuteReader(mDbCmd, sqlQuery);
            while (DbDataReader.Read())
            {
                XmlElement eLine = XmlProcess.AddVchLineItemElm(xmlInDoc);
                DbDataProvider.GetValuesToXml(DbDataReader, ref eLine,
                    tagSystem.Users.UserGrpID,
                    tagSystem.Users.IsAccRight,
                    tagSystem.Users.IsAdd,
                    tagSystem.Users.IsEdit,
                    tagSystem.Users.IsDelete,
                    tagSystem.Users.IsPrint,
                    tagSystem.Users.IsShow,
                    tagSystem.Users.ModuleID,
                    tagSystem.Users.IsSystem,
                    tagSystem.Users.ModuleName);
            }
            DbDataReader.Close();
            return xmlInDoc;
        }

        protected override bool OnCheckAllowItemToAdd(System.Collections.Generic.List<DbDataCatalogs.FieldInfo> pListValuesToCheck)
        {
            return true;
        }
        
        protected override bool OnCheckAllowDelItem(object pValuePrkID)
        {
            return true;
        }

        protected override bool OnCheckAllowItemToUpdate(object pValuePrkID, System.Collections.Generic.List<FieldInfo> pListValuesToCheck)
        {
            return true;
        }
        #endregion
        #region User methods
    
        #endregion

	}
}
